package com.asset.service.impl;

import com.asset.mapper.AssetMapper;
import com.asset.mapper.DepreciationMapper;
import com.asset.model.entity.Asset;
import com.asset.model.entity.DepDetail;
import com.asset.model.entity.DepreciationDetail;
import com.asset.service.DepreciationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepreciationServiceImpl implements DepreciationService {
    @Autowired
    private DepreciationMapper depreciationMapper;

    @Autowired
    private AssetMapper assetMapper;

    /**
     * 定时任务，定期执行折旧，存放到折旧记录表
     * @return
     */
    @Override
    public void update() {
        //判断状态，调拨，升级改造，封存状态不进行更新
        //判断本月入账，本月不折旧
        //已经折旧完的不折旧


        //获取符合条件的资产
        ArrayList<Asset> assets = assetMapper.getDepAsset();

        ArrayList<DepreciationDetail> depreciationDetails =new ArrayList<>();
        for (Asset asset : assets) {
            DepreciationDetail depreciationDetail = insertAsset(asset);
            depreciationDetails.add(depreciationDetail);
        }

        //从折旧记录表里找上次未折旧（系统自动折旧，但是人员没有确认的资产）的资产（比如根据操作人为空的记录）
        List<DepDetail> maxDeps = depreciationMapper.getMaxCumulativeDepreciation();
        List<DepreciationDetail> newDetails = new ArrayList<>();
        for (DepDetail depDetail : maxDeps) {
            newDetails.add(depreciationMapper.getNewDetail(depDetail));
        }

        //可以筛选第一次要折旧的新资产（不做）

        //更新depreciationDetails里的数据,获取本次要折旧的所有资产
        for (DepreciationDetail depreciationDetail : depreciationDetails) {
            for (DepreciationDetail newDetail : newDetails) {
                //看看资产以前折旧过吗
                if(newDetail.getAssetId().equals(depreciationDetail.getAssetId())){
                    //以前折旧过，更新数据
                    depreciationDetail = newDetail;
                }
            }
        }

        //将资产列表放在工具类里计算折旧

    }

    //在折旧详情对象中存放资产对象和对其他属性初始化
    private static DepreciationDetail insertAsset(Asset asset) {
        DepreciationDetail depreciationDetail = new DepreciationDetail();
        //存放资产对象
        depreciationDetail.setAsset(asset);
        //明细存放资产id
        depreciationDetail.setAssetId(asset.getId());
        //明细存放原值
        depreciationDetail.setOriginalValue(asset.getOriginalValue());
        //明细存放折旧前净值
        depreciationDetail.setNetValueBefore(asset.getNetValue());
        //存放折旧后净值
        depreciationDetail.setNetValueAfter(0.0);
        //存放本期折旧金额
        depreciationDetail.setCurrentDepreciation(0.0);
        //存放累计折旧金额
        depreciationDetail.setCumulativeDepreciation(asset.getCumulativeDepreciation());
        //存放剩余折旧金额
        depreciationDetail.setRemainingValue(0.0);
        //存放累计折旧期数
        depreciationDetail.setMonthsCumulative(asset.getDepreciationMonths());
        //存放剩余折旧期数
        depreciationDetail.setMonthsRemaining(asset.getRemainingMonths());
        return depreciationDetail;
    }
}
